package chapter06;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

public class CASLockTest {

}
@Slf4j
class CasLock{
    // 0没加锁 1加锁
    private AtomicInteger state=new AtomicInteger(0);

    public void lock(){
        while (true){
            if(state.compareAndSet(0,1)){
                log.debug("add lock success");
                break;
            }
        }
    }
    public void unlock(){
        state.set(0);
        log.debug("release lock success");
    }
}